﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class EditUnit : Form
    {
        private int id;
        private UnitDao dao;
        public EditUnit()
        {
            InitializeComponent();
        }

        public EditUnit(int id)
        {
            InitializeComponent();
            this.id = id;
            dao = new UnitDao();
            load(id);
        }

        private void load(int id)
        {
            DataRow row = dao.getUnitById(id);
            if (row != null)
            {
                txtName.Text = row["NAME"].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() != "")
            {
                if (dao.updateUnit(id, txtName.Text.Trim()))
                {
                    Result re = new Result(UserConstant.SAVE_SUCCESSFUL);
                    re.ShowDialog();
                    re.Dispose();
                    this.Dispose();
                }
                else
                {
                    Error er = new Error(UserConstant.SAVE_FAIL);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
            else
            {
                Error er = new Error(UserConstant.INPUT_VALIDATE);
                er.ShowDialog();
                er.Dispose();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
